import { Inject } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Injectable} from "@angular/core";
import { Observable } from "rxjs";
import { map, pluck, switchMap } from "rxjs/operators";
import {  SongSheet } from "./data-types/common.types";
import { API_CONFIG, ServicesModule } from "./services.module";
import { SongService } from "./song.service";

 
//  歌单服务
@Injectable({
  providedIn: ServicesModule
})
export class SheetService {
   

  constructor(private http:HttpClient,@Inject(API_CONFIG) private uri:string,private songService: SongService) { }


  // 3.获取歌单详情
  getSongSheetDetail(id:number):Observable<SongSheet[]>{
    // 
    const params  = new HttpParams().set('id',id.toString())
    return this.http.get(this.uri+'playlist/detail',{params})
    .pipe(map((res:any)=>res.playlist))
  }
  
  //2. 获取歌单详情处理去获取每一首歌url
    playSheet(id:number):Observable<any>{
      // 4.获取歌单详情
      return this.getSongSheetDetail(id)
      .pipe(pluck('tracks'),switchMap((tracks:any)=> this.songService.getSongList(tracks)))
    }
}
